﻿using CI.Interfaces;
using CI.Organism;

namespace CI.Mesopotamia
{
    /// <summary>
    /// Locations collision helper class
    /// </summary>
    public class LocationCollisions
    {
        /// <summary>
        /// Did the combined location and radius of the organisms collide
        /// </summary>
        /// <param name="organism1"></param>
        /// <param name="organism2"></param>
        /// <returns>false if they missed, true if they collied</returns>
        public static bool LocationsCollide(ILocation organism1, ILocation organism2)
        {
            // check x axis
            if (!CheckAxisCollision(organism1.XAxis, organism1.XRadius, organism2.XAxis, organism2.XRadius))
                return false;

            // check y axis
            if (!CheckAxisCollision(organism1.YAxis, organism1.YRadius, organism2.YAxis, organism2.YRadius))
                return false;

            // check z axis
            if (!CheckAxisCollision(organism1.ZAxis, organism1.ZRadius, organism2.ZAxis, organism2.ZRadius))
                return false;

            return true;
        }

        /// <summary>
        /// Check axis collison of the two organisms
        /// </summary>
        /// <param name="org1Axis"></param>
        /// <param name="org1Radius"></param>
        /// <param name="org2Axis"></param>
        /// <param name="org2Radius"></param>
        /// <returns>false if we missed, true it the axis and raidus collided</returns>
        private static bool CheckAxisCollision(float org1Axis, float org1Radius, float org2Axis, float org2Radius)
        {
            // if check x > childOrganism then adjust
            if (org1Axis > org2Axis)
            {
                // if the check location with - radius is still above the other childOrganism then we have missed
                if ((org1Axis - org1Radius) > (org2Axis + org2Radius))
                    return false;
            }
            // if check x < childOrganism then adjust
            else
            {
                if ((org1Axis + org1Radius) < (org2Axis - org2Radius))
                    return false;
            }

            return true;
        }
    }
}
